#ifndef TIMER_H
#define TIMER_H

namespace fx {

    FX_DECLARE_CLASS(Timer);
    class Timer
        : public boost::noncopyable
        , public boost::enable_shared_from_this<Timer>
    {
    public:
        typedef boost::signal<void (boost::uint32_t timer_count)> signal_t;
        typedef boost::signals::connection connection_t;
        typedef signal_t::slot_function_type handler_t;

    public:
        virtual ~Timer() {}

        static TimerPtr Create(
            boost::uint32_t interval_in_ms, boost::uint32_t max_timer_count);

    public:
        void Start();
        void Stop();

        void SetIntervalInMS(boost::uint32_t interval_in_ms);
        boost::uint32_t GetIntervalInMS() const;

        void SetMaxTimerCount(boost::uint32_t max_timer_count);
        boost::uint32_t GetMaxTimerCount() const;

        connection_t AddTimerHandler(handler_t handler);

    protected:
        explicit Timer(
            boost::uint32_t interval_in_ms, boost::uint32_t max_timer_count);

        void StartLocalTimer();
        void OnTimerElapsed(const boost::system::error_code& error);

    protected:

        boost::uint32_t interval_in_ms_;
        boost::uint32_t max_timer_count_;
        boost::uint32_t curr_timer_count_;

        boost::asio::deadline_timer internal_timer_;
        signal_t timer_signal_;
    };

}

#endif//TIMER_H
